package com.wyx.bbs.controller;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.URL;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/tool/oss")
public class Upload {
Log log = LogFactory.getLog(Upload.class);

//阿里云OSS地址，这里看根据你的oss选择
protected static String endpoint = "oss-cn-shenzhen.aliyuncs.com";
//阿里云OSS账号
protected static String accessKeyId  = "LTAI5tJm6bFukwjGuHeSpUWp";
//阿里云OSS密钥
protected static String accessKeySecret  = "W4shyHo588mr9mWQvVXElL4V0kJhSh";
//阿里云OSS上的存储块bucket名字
protected static String bucketName  = "wyx-bbs-file";

//阿里云图片文件存储目录
private String homeimagedir = "somnus/";


//@Autowired
//private OSSClientUtil ossClientUtil;
////获取单图片路径
//String imageUrl = ossClientUtil.checkImage(image);  //image 为MultipartFile类型
////多图片路径
//String imageUrlList = ossClientUtil.checkList(imageList); //imageList为List<MultipartFile>  返回 "，"分割的图片路径


// 未对异常进行处理
@RequestMapping(value = "/deleteImage")
@ResponseBody
public int deleteImage(@RequestParam String imageName) {
    //文件名
    String fileLastName = homeimagedir+imageName.substring(imageName.lastIndexOf("/")+1);
    System.out.println("fileLastName:"+fileLastName);
    OSSClient client = new OSSClient(endpoint, accessKeyId, accessKeySecret);
    client.deleteObject(bucketName, fileLastName);
    client.shutdown();
    return 200;
}



//处理文件上传
@RequestMapping(value="/homeImageUpload", method = RequestMethod.POST)
@ResponseBody
public String uploadFile(HttpServletRequest request, @RequestParam("file")MultipartFile file) throws IOException {
//
    System.out.println("file:"+file);
    OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
// 获取文件名
    String fileName = file.getOriginalFilename();
    System.out.println("fileName:"+fileName.toString());
// 获取文件后缀名
    String suffixName = fileName.substring(fileName.lastIndexOf("."));

    //文件名
    String fileLastName = fileName.substring(0, fileName.indexOf("."));

    System.out.println("suffixName:"+fileLastName);
// 最后上传生成的文件名 + new SecureRandom().nextInt(0x0400)
    String finalFileName = fileLastName + "" + suffixName;
// oss中的文件夹名
//    String objectName = sdf.format(new Date()) + "/" + finalFileName;
    String objectName = homeimagedir + finalFileName;
// 创建上传文件的元信息，可以通过文件元信息设置HTTP header(设置了才能通过返回的链接直接访问)。
    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setContentType("image/jpg");
//    objectMetadata.setContentDisposition("attachment;");
// 文件上传
    ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(file.getBytes()),objectMetadata);
// 设置URL过期时间为1小时。
    Date expiration = new Date(System.currentTimeMillis() + 3600 * 1000 * 24 * 10);
    String url = ossClient.generatePresignedUrl(bucketName, objectName, expiration).toString();
    ossClient.shutdown();
    System.out.println("阿里云OSS的文件url:{}"+ url);
    return url;
}
}
